package com.sixth.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixth.core.entity.Medicines;
import com.sixth.core.util.WebUtil;
import com.sixth.core.vo.R;
import com.sixth.stock.mapper.MedicinesMapper;
import com.sixth.stock.service.MedicinesService;
import com.sixth.stock.vo.DespensingVo;
import com.sixth.stock.vo.MedicinesForInventoryVo;
import com.sixth.stock.vo.MedicinesVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【stock_medicines(药品信息表)】的数据库操作Service实现
* @createDate 2024-08-14 20:33:04
*/
@Service
public class MedicinesServiceImpl extends ServiceImpl<MedicinesMapper, Medicines>
    implements MedicinesService{
@Resource
private MedicinesMapper medicinesMapper;
    @Override
    public R<IPage<Medicines>> selectByPage(MedicinesVo medicinesVo) {
        Integer pageNum = medicinesVo.getPageNum();
        Integer pageSize = medicinesVo.getPageSize();
        if (pageNum==null||pageNum<=0||pageSize==null||pageSize<=0){
            pageNum=1;
            pageSize=5;
        }
        IPage<Medicines> page =new Page<>(pageNum,pageSize);
        QueryWrapper<Medicines> wrapper = new QueryWrapper<>();


        if (medicinesVo.getMedicinesType()!=null&&medicinesVo.getMedicinesType()!=""){
            wrapper.eq("medicines_type",medicinesVo.getMedicinesType());
        }
        if (medicinesVo.getPrescriptionType()!=null&&medicinesVo.getPrescriptionType()!=""){
            wrapper.eq("prescription_type",medicinesVo.getPrescriptionType());
        }
        if (medicinesVo.getKeywords()!=null&&medicinesVo.getKeywords()!=""){
            wrapper.like("keywords",medicinesVo.getKeywords());
        }
        if (medicinesVo.getProducterId()!=null&&medicinesVo.getProducterId()>=0){
            wrapper.eq("producter_id",medicinesVo.getProducterId());
        }
        if (medicinesVo.getMedicinesName()!=null&&medicinesVo.getMedicinesName()!=""){
            wrapper.like("medicines_name",medicinesVo.getMedicinesName());
        }
        if (medicinesVo.getStatus()!=null&&medicinesVo.getStatus()!=""){
            wrapper.eq("status",medicinesVo.getStatus());
        }
        if (medicinesVo.getStatus()==null||medicinesVo.getStatus()==""){
            wrapper.eq("status","0");
        }
        wrapper.eq("del_flag",0);//查询未删除的数据
        //根据创建时间降序排序，之后再根据id升序排序
        wrapper.orderByDesc("create_time").orderByAsc("medicines_id");
        IPage<Medicines> medicinesIPage = medicinesMapper.selectPage(page, wrapper);
        return R.ok(medicinesIPage);
    }
    @Override
    public R<IPage<Medicines>> selectByPage2(MedicinesForInventoryVo medicinesVo) {
        Integer pageNum = medicinesVo.getPageNum();
        Integer pageSize = medicinesVo.getPageSize();
        if (pageNum==null||pageNum<=0||pageSize==null||pageSize<=0){
            pageNum=1;
            pageSize=5;
        }
        IPage<Medicines> page =new Page<>(pageNum,pageSize);
        QueryWrapper<Medicines> wrapper = new QueryWrapper<>();


        if (medicinesVo.getMedicinesType()!=null&&medicinesVo.getMedicinesType()!=""){
            wrapper.eq("medicines_type",medicinesVo.getMedicinesType());
        }
        if (medicinesVo.getPrescriptionType()!=null&&medicinesVo.getPrescriptionType()!=""){
            wrapper.eq("prescription_type",medicinesVo.getPrescriptionType());
        }
        if (medicinesVo.getKeywords()!=null&&medicinesVo.getKeywords()!=""){
            wrapper.like("keywords",medicinesVo.getKeywords());
        }
        if (medicinesVo.getProducterId()!=null&&medicinesVo.getProducterId()>=0){
            wrapper.eq("producter_id",medicinesVo.getProducterId());
        }
        if (medicinesVo.getMedicinesName()!=null&&medicinesVo.getMedicinesName()!=""){
            wrapper.like("medicines_name",medicinesVo.getMedicinesName());
        }
        if (medicinesVo.getDateRange()!=null&&medicinesVo.getDateRange().size()==2){
            wrapper.between("create_time",medicinesVo.getDateRange().get(0),medicinesVo.getDateRange().get(1));
        }
        wrapper.eq("del_flag",0);//查询未删除的数据
        //根据创建时间降序排序，之后再根据id升序排序
        wrapper.orderByDesc("create_time").orderByAsc("medicines_id");
        IPage<Medicines> medicinesIPage = medicinesMapper.selectPage(page, wrapper);
        return R.ok(medicinesIPage);
    }

    @Override
    public R<String> batchDelete(Long[] ids)  {
        //批量修改字段delflag为1,调用自带的方法
        boolean flag = false;
        if (ids.length>0){
            for (Long id:ids){
                Medicines medicines = medicinesMapper.selectById(id);
                if (medicines!=null){
                    medicines.setDelFlag(String.valueOf(1));
                    medicinesMapper.updateById(medicines);
                    flag=true;
                }
                else {
                    return R.error(id+"id处删除失败，请检查id是否正确");
                }
            }
        }
        return flag ? R.ok("删除成功") : R.error("删除失败");
    }



    @Override
    public R<IPage<Medicines>> selectInit() {
        IPage<Medicines> page =new Page<>(1,5);
        QueryWrapper<Medicines> wrapper = new QueryWrapper<>();
        wrapper.eq("del_flag",0);//查询未删除的数据
        wrapper.orderByDesc("create_time").orderByAsc("medicines_id");
        return R.ok(medicinesMapper.selectPage(page,wrapper));
    }

    @Override
    public List<Medicines> listAvailable() {
        //查询所有未删除的数据
        List<Medicines> list = medicinesMapper.selectList(new QueryWrapper<Medicines>().eq("del_flag",0).eq("status","0"));
        return list;
    }

    @Override
    public R updateStock(Long id, Integer stock)  {
        Medicines medicines = medicinesMapper.selectById(id);
        if (medicines!=null){
            medicines.setMedicinesStockNum(stock);
            String username = WebUtil.getUsername();
            medicines.setUpdateBy(username);
            medicines.setUpdateTime(new Date());
            int i = medicinesMapper.updateById(medicines);
            return i>0 ? R.ok("修改成功") : R.error("修改失败");
        }
        boolean flag = medicinesMapper.updateById(medicines)>0;
        return flag ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R<String> deleteById(Long id) {
        Medicines medicines = medicinesMapper.selectById(id);
        if (medicines!=null){
            medicines.setDelFlag("1");
            medicinesMapper.updateById(medicines);
            return R.ok("删除成功");
        }
        return R.error("未找到该药品信息");
    }
    @Override
    public R despensing(List<DespensingVo> despensingVos){
        if (despensingVos!=null&&despensingVos.size()>0){
            for (DespensingVo despensingVo : despensingVos) {
               Medicines medicines = medicinesMapper.selectOne(new QueryWrapper<Medicines>().eq("medicines_name",despensingVo.getItemName()));
                if (medicines!=null){
                    Integer medicinesStockNum = medicines.getMedicinesStockNum();
                    if (medicinesStockNum>=despensingVo.getItemNum()){
                        medicines.setMedicinesStockNum(medicinesStockNum-despensingVo.getItemNum());
                        medicinesMapper.updateById(medicines);
                       }
                    else {return R.error(despensingVo.getItemName()+":修改失败，库存不足");}
                }
                else {return R.error(despensingVo.getItemName()+":修改失败，未找到该药品信息");}
            }
            return R.ok("修改成功");

        }
        return R.error("修改失败，请检查数据是否合理");
    }

}




